<?php

namespace app\admin\controller;

use app\admin\common\Base;
use app\admin\model\Solution as SolutionModel;

class Solution extends Base {
    public function index($keyword = '',$page = 1,$limit = 10){
        if (is_post()){
            if (!empty($keyword)){
                $where[] = ['title','like','%' . $keyword . '%'];
            }
            $where[] = ['is_del','=',0];

            $data = $this->getListData('solution',$where,'sort','asc',$page,$limit);

            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$data['count'],'data'=>$data['list'],'current_page'=>$page]);
        }
        return $this->fetch();
    }

    public function add(){
        if (is_post()){
            //实例化模型
            $model = new SolutionModel();
            //接收数据
            $data = input('post.');
            //执行数据库操作
            $result = $model->add($data);

            return json(['code'=>$result['code'],'msg'=>$result['msg']]);

        }
        $all_products = $this->getAllProducts();
        $this->assign('all_products',json_encode($all_products));
        return $this->fetch();
    }

    public function edit($id){
        if (is_post()){
            //实例化模型
            $model = new SolutionModel();
            //接收数据
            $data = input('post.');
            //执行数据库操作
            $result = $model->edit($id,$data);

            return json(['code'=>$result['code'],'msg'=>$result['msg']]);
        }

        $all_products = $this->getAllProducts();
        $this->assign('all_products',json_encode($all_products));

        $info = db('solution')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);

        $related_products = array_filter(explode(',',$info['related_products']));
        $this->assign('related_products',json_encode($related_products));
        return $this->fetch();
    }

    /**
     * 复制
     * @param $data string 要复制的记录内容 json字符串
     * @return \think\response\Json
     */
    public function copy($data){
        if (is_post()){
            $data = json_decode($data,true);
            $ids = [];
            foreach ($data as $k => $v){
                array_push($ids,intval($v['id']));
                $data[$k]['create_time'] = time();
                $data[$k]['update_time'] = time();

                if (!empty($v['seo_link'])){
                    $data[$k]['seo_link'] = $v['seo_link'] . mt_rand(0,99);
                }

                unset($data[$k]['id']);
            }
            $ids = implode(',',$ids);
            $result = db('solution')->insertAll($data);
            if ($result){
                insert_system_log('复制案例：' . $ids);
                return json(['code' => 'suc', 'msg' => '复制成功']);
            }else{
                return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
            }
        }
    }

    public function del($id){
        if (is_post()){
            if ($this->delRecord('solution',$id,'删除案例')){
                return json(['code' => 'suc','msg' => '已删除']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function changeStatus($id,$type,$field){
        if (is_post()){
            switch ($field){
                case 1:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
                    break;
                case 2:
                    $log_action = $type==1 ? '推荐' : '取消推荐';
                    $action_str = $type==1 ? '已推荐' : '取消推荐';
                    break;
                default:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
            }
            if ($this->changeRecordStatus('solution',$id,$field,$type)){
                insert_system_log($log_action . '案例：' . $id);
                return json(['code'=>'suc','msg'=>$action_str . '案例']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    /**
     * 获取所有产品
     * @return mixed
     */
    public function getAllProducts(){

        $where[] = ['is_del','=',0];
        $where[] = ['status','=',1];
        $all_products = db('product')
            ->where($where)
            ->field('id as value,name as title')
            ->select();
        foreach ($all_products as $k => $v){
            $all_products[$k]['disabled'] = "";
            $all_products[$k]['checked'] = "";
        }
        return $all_products;
    }

    public function upload(){
        $file = request()->file('file');
        $info = $this->uploadsImg($file,'uploads/solution/');
        if ($info !== false){
            return json(['code' => 'suc','msg' => '图片上传成功','url' => $info['path']]);
        }else{
            return json(['code' => 'err','msg' => '图片上传失败，请重试']);
        }
    }
}